package edu.uba.fcen.estimacion.database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class QueryDB {
	
	private Connection connection;

	public QueryDB() {
			DataBaseBuilder geo = new DataBaseBuilder(DataBaseBuilder.GEO_DB);
			this.connection = geo.create();
//			this.connection = DriverManager.getConnection("jdbc:hsqldb:mem:geo", "sa", "");
	}
	
	public boolean exist(String word) {
		String [] querys = {
				"SELECT count(*) FROM Cities WHERE City = 'WORD'",
				"SELECT count(*) FROM Country WHERE Country = 'WORD'",
				"SELECT count(*) FROM Regions WHERE Region = 'WORD'",
				"SELECT count(*) FROM Names WHERE Name = 'WORD'",
				"SELECT count(*) FROM Miscellaneous WHERE Word = 'WORD'"
		};
		boolean exist = false; 
		try {
			Statement statement = this.connection.createStatement();
			String query;
			for (int i=0; i<querys.length && !exist; i++) {
				query = querys[i];
				query = query.replaceAll("WORD", word);
				ResultSet rs = statement.executeQuery(query);
				if (rs.next()) {
					exist = exist || (rs.getInt(1) > 0);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return exist;
	}
	
}
